package com.yfp.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.goods.domain.ComboGoods;
import com.yfp.goods.domain.Goods;
import com.yfp.goods.domain.GoodsType;
import com.yfp.goods.domain.ImgResource;
import com.yfp.goods.domain.vo.ComboGoodsVo;
import com.yfp.goods.mapper.ComboGoodsMapper;
import com.yfp.goods.mapper.GoodsMapper;
import com.yfp.goods.mapper.GoodsTypeMapper;
import com.yfp.goods.mapper.ImgResourceMapper;
import com.yfp.goods.service.ComboGoodsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 套餐商品表(ComboGoods)表服务实现类
 *
 *
 * @since 2023-12-27 15:35:02
 */
@Service("comboGoodsService")
public class ComboGoodsServiceImpl implements ComboGoodsService {
    @Resource
    private ComboGoodsMapper comboGoodsMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Resource
    private ImgResourceMapper imgResourceMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param comboGoodsId 主键
     * @return 实例对象
     */
    @Override
    public ComboGoods queryById(String comboGoodsId) {
        return this.comboGoodsMapper.queryById(comboGoodsId);
    }

    /**
     * 分页查询
     *
     * @param comboGoods 筛选条件
     * @return 查询结果
     */
    @Override
    public List<ComboGoods> queryByPage(ComboGoods comboGoods) {
        return this.comboGoodsMapper.queryAllByLimit(comboGoods);
    }

    /**
     * 新增数据
     *
     * @param comboGoodsVo 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(List<ComboGoodsVo> comboGoodsVo) {
        if (CollUtil.isEmpty(comboGoodsVo)) throw new ServiceException("新增失败");
        Snowflake snowflake = new Snowflake(1);
        for (ComboGoodsVo goodsVo : comboGoodsVo) {

            String comboGoodsId = String.valueOf(snowflake.nextId());

            ComboGoods comboGoods = new ComboGoods();
            comboGoods.setComboGoodsId(comboGoodsId);
            comboGoods.setGoodsId(goodsVo.getGoodsId());
            comboGoods.setComboId(goodsVo.getComboId());
            comboGoods.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());

            this.comboGoodsMapper.insert(comboGoods);
        }
        return R.ok();
    }

    /**
     * 修改数据
     *
     * @param comboGoods 实例对象
     * @return 实例对象
     */
    @Override
    public ComboGoods update(ComboGoods comboGoods) {
        this.comboGoodsMapper.update(comboGoods);
        return this.queryById(comboGoods.getComboGoodsId());
    }

    /**
     * 根据套餐id查询绑定的商品信息
     *
     * @param comboId 组合id
     * @return {@link Goods}
     */
    @Override
    public List<Goods> queryByComboId(String comboId) {
        List<ComboGoods> comboGoods = this.comboGoodsMapper.queryByComboId(comboId);

        if (CollUtil.isEmpty(comboGoods)) return Collections.emptyList();


        List<Goods> goodsList = CollUtil.newArrayList();
        for (ComboGoods comboGood : comboGoods) {

            Goods goods = goodsMapper.queryById(comboGood.getGoodsId());
            if (ObjectUtil.isNull(goods)) continue;

            GoodsType goodsType = this.goodsTypeMapper.queryById(goods.getGoodsTypeId());
            if (ObjectUtil.isNotNull(goodsType)) goods.setGoodsTypeName(goodsType.getGoodsTypeName());

            ImgResource imgResource = this.imgResourceMapper.queryById(goods.getFrontImgId());

            if (ObjectUtil.isNotNull(imgResource)) goods.setFrontImgUrl(imgResource.getUrl());


            goods.setComboGoodsId(comboGood.getComboGoodsId());
            goodsList.add(goods);
        }
        return goodsList;
    }

    /**
     * 按商品id计数
     *
     * @param goodsId 货物id
     * @return long
     */
    @Override
    public List<ComboGoods> queryByGoodsId(String goodsId) {
        return this.comboGoodsMapper.queryByGoodsId(goodsId);
    }

    /**
     * 删除套餐商品
     *
     * @param comboGoodsId 组合商品id
     * @return {@link R}
     */
    @Override
    public boolean deleteComboGoods(String comboGoodsId) {

        if (CharSequenceUtil.isEmpty(comboGoodsId)) throw new ServiceException("缺少必填参数");

        return this.comboGoodsMapper.deleteComboGoods(comboGoodsId) > 0 ;
    }
}
